package com.zcdyy.rentalofhouse.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.util.Util;
import com.zcdyy.rentalofhouse.R;
import com.zcdyy.rentalofhouse.application.MyApplication;
import com.zcdyy.rentalofhouse.bean.AllRental;
import com.zcdyy.rentalofhouse.bean.OrderInfo;
import com.zcdyy.rentalofhouse.bean.SaleHouse;
import com.zcdyy.rentalofhouse.bean.ShopRent;
import com.zcdyy.rentalofhouse.bean.SingleRent;
import com.zcdyy.rentalofhouse.listener.OnItemClickListener;
import com.zcdyy.rentalofhouse.ui.activity.LoginActivity;
import com.zcdyy.rentalofhouse.utils.AppManager;
import com.zcdyy.rentalofhouse.utils.BmobUtils;
import com.zcdyy.rentalofhouse.utils.ImageLoaderUtils;
import com.zcdyy.rentalofhouse.utils.RentalUtils;
import com.zcdyy.rentalofhouse.utils.Utils;
import com.zcdyy.rentalofhouse.views.MyAlertDialog;

import java.util.List;

/**
 * Created by chuan on 2017/3/12.
 */

public class MyRentalAdapter extends RecyclerView.Adapter<MyRentalAdapter.MyViewHolder>{
    private List<OrderInfo> list;
    private Context context;
    private OnItemClickListener onItemClickListener;
    private BmobUtils bmobUtils;
    public MyRentalAdapter(Context context, List<OrderInfo> list) {
        this.list = list;
        this.context = context;
        bmobUtils = new BmobUtils(context);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void addList(List<OrderInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public MyRentalAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyRentalAdapter.MyViewHolder holder = new MyRentalAdapter.MyViewHolder(
                LayoutInflater.from(context).inflate(R.layout.item_publishd_show, parent, false), onItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyRentalAdapter.MyViewHolder holder, final int position) {
        ImageLoaderUtils.loadImage(context,list.get(position).getPic().getFileUrl(),holder.head);
        holder.title.setText(list.get(position).getTitle());
        holder.address.setText(list.get(position).getCity());
        holder.price.setText(list.get(position).getRent_money());
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            deleteDialog(list.get(position));
            }
        });
        holder.huishou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                huishouDialog(list.get(position));
            }
        });
    }

    /**
     * 删除提示框
     * @param orderInfo
     */
    private void deleteDialog(final OrderInfo orderInfo) {
        MyAlertDialog logDialog = new MyAlertDialog(context);
        logDialog.builder()
                .setTitle("是否要删除")
                .setMsg("删除之后无法恢复！！！")
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                })
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bmobUtils.deleteRental(orderInfo);//删除发布然后重新获取
                        list.remove(orderInfo);
                        //delete(orderInfo);
                    }
                })
                .show();
    }

    public void delete(OrderInfo o){
        RentalUtils.list_order.remove(o);
        switch (o.getType()){
            case "0":
                for (AllRental r:RentalUtils.list_allRental){
                    if (r.getId().equals(o.getId())){
                        RentalUtils.list_allRental.remove(r);
                    }
                }
                break;
            case "1":
                for (SaleHouse s:RentalUtils.list_saleHouse){
                    if (s.getId().equals(o.getId())){
                        RentalUtils.list_saleHouse.remove(s);
                    }
                }
                break;
            case "2":
                for (SingleRent sr:RentalUtils.list_singleRent){
                    if (sr.getId().equals(o.getId())){
                        RentalUtils.list_singleRent.remove(sr);
                    }
                }
                break;
            case "3":
                for (ShopRent sp:RentalUtils.list_shopRent){
                    if (sp.getId().equals(o.getId())){
                        RentalUtils.list_shopRent.remove(sp);
                    }
                }
                break;
        }
    }

    /**
     * 回收提示框
     * @param orderInfo
     */
    private void huishouDialog(final OrderInfo orderInfo) {
        MyAlertDialog logDialog = new MyAlertDialog(context);
        logDialog.builder()
                .setTitle("是否要回收")
                .setMsg("确定此房源已经租出或者卖出？")
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                })
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bmobUtils.deleteRental(orderInfo);//删除发布然后重新获取
                        list.remove(orderInfo);
                        //delete(orderInfo);
                    }
                })
                .show();
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private OnItemClickListener onItemClickListener;
        private ImageView head;
        private TextView title;
        private TextView address;
        private TextView price;
        private TextView delete;
        private TextView huishou;
        private TextView createTime;

        public MyViewHolder(View itemView, OnItemClickListener onItemClickListener) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.onItemClickListener = onItemClickListener;
            head = Utils.findViewsById(itemView,R.id.header);
            title = Utils.findViewsById(itemView,R.id.tv_mess_name);
            address = Utils.findViewsById(itemView,R.id.tv_mess_qu);
            price = Utils.findViewsById(itemView,R.id.tv_rent_money);
            delete = Utils.findViewsById(itemView,R.id.tv_delete);
            huishou = Utils.findViewsById(itemView,R.id.tv_huishou);
            createTime = Utils.findViewsById(itemView,R.id.tv_createTime);
            createTime.setVisibility(View.GONE);
            delete.setVisibility(View.VISIBLE);
            huishou.setVisibility(View.VISIBLE);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
}
